//
//  SearchViewController.m
//  NomalTest
//
//  Created by 陈小东 on 15/6/17.
//  Copyright (c) 2015年 杭州时投信息科技@陈小东. All rights reserved.
//

#import "SearchViewController.h"

@interface SearchViewController ()
<UISearchResultsUpdating>
@property (nonatomic,retain) UISearchController *searchController;
@property (nonatomic,strong) NSMutableArray *oldDataArray;
@property (nonatomic,strong) NSMutableArray *useDataArray;

@end

static NSString *kSearchCellIndentifier = @"kSearchCellIndentifier";

@implementation SearchViewController

- (NSMutableArray *)oldDataArray{
    if(!_oldDataArray){
        _oldDataArray = [NSMutableArray array];
        for(NSInteger i = 0;i < 1000;i++){
            [_oldDataArray addObject:[NSString stringWithFormat:@"%ld",i]];
        }
    }
    return _oldDataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"SearchViewController";
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:kSearchCellIndentifier];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
                                                                                           target:self
                                                                                           action:@selector(search)];
    
}

- (void)search{
    self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
    self.searchController.searchBar.frame = CGRectMake(20, 0, self.view.frame.size.width - 40, 44);
    self.searchController.searchBar.placeholder = @"输入查找内容";
//    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchResultsUpdater = self;
    [self presentViewController:self.searchController animated:YES completion:nil];
}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    //谓词检测
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"self contains [cd] %@", searchController.searchBar.text];
    //将所有和搜索有关的内容存储到arr数组
    self.useDataArray = [NSMutableArray arrayWithArray:
                         [self.oldDataArray filteredArrayUsingPredicate:predicate]];
    [self.useDataArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        if ([obj1 integerValue] > [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        if ([obj1 integerValue] < [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    }];
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.useDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSearchCellIndentifier
                                                            forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",self.useDataArray[indexPath.row]];
    return cell;
}

@end
